package com.manager.controller;

import com.manager.domain.Teacher;
import com.manager.service.TeacherService;
import java.util.Scanner;

public class TeacherController {
    private Scanner sc = new Scanner(System.in);
    private TeacherService teacherService = new TeacherService();
    public void start() {
        int input;
        while (true) {
            System.out.println("--------欢迎来到 <老师> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加老师  2.删除老师  3.修改老师  4.查看老师  5.退出");
            input = sc.nextInt();
            switch (input) {
                case 1:
                    //System.out.println("添加老师");
                    addTeacher();
                    break;
                case 2:
                    //System.out.println("删除老师");
                    delTeacher();
                    break;
                case 3:
                    //System.out.println("修改老师");
                    upDataTeacher();
                    break;
                case 4:
                    //System.out.println("查看老师");
                    findAllTeacher();
                    break;
                case 5:
                    //System.out.println("退出");
                    System.out.println("欢迎下次使用老师管理系统!");
                    return;
                default:
                    System.out.println("输入有误,请重新输入!");
                    break;
            }
        }

    }

    private void findAllTeacher() {
        Teacher[] tec = teacherService.findAllTeacher();
        if (tec==null){
            System.out.println("没有信息 请添加后重试!");
            return;
        }
        System.out.println("学号\t姓名\t年龄\t生日");
        for (int i = 0; i < tec.length; i++) {
            Teacher teacher = tec[i];
            if (teacher != null)
            System.out.println(teacher.getId() + "\t" + teacher.getName() + "\t" + teacher.getAge() + "岁\t" + teacher.getBirthday());
        }
    }
    private void upDataTeacher() {
        String id = absPd("未找到 请重新输入", "upD");
        if (id!=null){
            Teacher newTec = getTeacher(id);
            teacherService.upDataTeacher(id, newTec);
            System.out.println("更新成功");
        }
    }



    private void delTeacher() {
        String id = absPd("未找到 请重新输入", "del");
        if (id!=null){
            teacherService.delTeacher(id);
            System.out.println("删除成功");
        }
    }

    private void addTeacher() {
        String id = absPd("已存在 请重新输入", "add");
        if (id !=null){
            Teacher newTec = getTeacher(id);
            boolean isSen = teacherService.addTeacher(newTec);
            if (isSen) {
                System.out.println("添加成功");
            } else {
                System.out.println("添加失败,容器已满");
            }
        }
    }
    private Teacher getTeacher(String id) {
        System.out.println("请输入姓名");
        String name = sc.next();
        System.out.println("请输入年龄");
        int age = sc.nextInt();
        System.out.println("请输入生日");
        String birthday = sc.next();
        return new Teacher(id, name, age, birthday);
    }
    private String absPd(String str,String flag) {
        String id;
        while (true) {
            System.out.println("请输入id");
            id = sc.next();
            if (id.equals("0")){
                return null;
            }
            boolean isBool = teacherService.isExists(id);
            boolean isFlag = flag.equals("add") ? isBool != true : isBool != false;
            if (isFlag) {
                return id;
            }
            System.out.println("id"+str+", 退出[0]");
        }
    }

}
